package com.fy.service.imbusiness.controller;

import java.util.List;

import com.fy.service.service.SkImUserDeviceService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fy.framework.common.model.R;
import com.fy.framework.component.config.LocaleMessage;
import com.fy.common.entity.SkImUserDevice;
import com.fy.common.enums.SkImCommonStatusEnum;
import com.fy.common.util.TokenUtil;
import com.fy.common.dto.UserDeviceDeleteDto;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhshuo
 * @date 2021/12/01 15:04
 **/
@Slf4j
@RestController
@Api(tags = "用户设备管理")
@AllArgsConstructor
@RequestMapping("/user/device")
public class SkImUserDeviceController {

    LocaleMessage localeMessage;

    SkImUserDeviceService skImUserDeviceService;

    /**
     * 用户设备列表
     * 
     * @return List<SkImUserDevice>
     */
    @PostMapping("/list")
    @ApiOperation("用户设备列表")
    public R<?>
        list() {
        List<SkImUserDevice> userDevices = skImUserDeviceService.selectList(
            new EntityWrapper<SkImUserDevice>().eq("sk_im_user_id", TokenUtil.getUserId()).eq("status", 0)
                .orderBy("last_login_time",
                false));
        return R.success(userDevices);
    }

    /**
     * 设备删除
     * 
     * @param deviceDeleteDto
     *            设备删除
     * @return R
     */
    @PostMapping("/delete")
    @ApiOperation("用户设备删除")
    public R<?> delete(@RequestBody UserDeviceDeleteDto deviceDeleteDto) {
        boolean update = skImUserDeviceService.updateById(new SkImUserDevice().setId(deviceDeleteDto.getDeviceId())
            .setStatus(SkImCommonStatusEnum.DELETED.getStatus()));
        return update ? R.successAlter(localeMessage.getMessage("im.common.delSuccess"))
            : R.failedAlter(localeMessage.getMessage("im.common.delFailed"));
    }

}
